<?php 

/**
 * 在用户确认登录之后进行反馈发送消息统计数据
 */
 header("content-type:text/html;charset=utf8");
    get_binding_info($_GET['device']);
    //查询该设备号是否绑定过微信openid
    function get_binding_info($device_sn){
        $pdo = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
        $pdo->exec('set names utf8');
        $sql = "SELECT * FROM binding WHERE device_sn = '".$device_sn."'";
        $device_data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
        statistics($device_data);
    }
    //微信接口调用凭证
    function voucher_token(){  
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2de38da107527240&secret=446fdd6335e31f0ceeaf63562ff03a4c";
            $access_token = json_decode(file_get_contents($url),true);
            return $access_token;
    }
    //统计时间
    function statistics($device_data){
        $pdo = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
        $pdo->exec('set names utf8');
        //获取设备号  此设备号对应的微信openid所绑定的账户u_id  以及时间
        $device_sn = $device_data['device_sn'];
        $user_sql = "SELECT * FROM user WHERE openid = '".$device_data['openid']."'";
        $user_data = $pdo->query($user_sql)->fetch(PDO::FETCH_ASSOC);
	    $u_id = $user_data['u_id'];
        //判断此设备号是否于此用户之前统计过   否：进行统计    是：获取第一次统计时间进行发送模板消息
        $select_sql = "SELECT * FROM statistics WHERE u_Id = $u_id AND device_sn = '".$device_sn."'";
	    $statistics_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
	    if (empty($statistics_data)) {
            $time = time();
            $insert_sql = "INSERT INTO statistics(u_id,device_sn,`time`) VALUES('$u_id','$device_sn','$time')";
            $pdo->exec($insert_sql);
        }else{
            $time = $statistics_data['time'];
        }
        //获取用户信息   用来判断是否关注过公众号
        $openid_data = get_openid_info($device_data['openid']);
        if ($openid_data['subscribe']==1) {
            sendMessage($time,$device_data['openid'],$device_data['device_sn']);
        }
    }
    //通过公众号向此用户发送模板消息告知绑定成功
    function sendMessage($time,$openid,$sn){
        $access_token = voucher_token();
        $token = $access_token['access_token'];
        $judge = substr($sn,0,2);
            if ($judge == 'H2') {
                $type = "HiSpot-2";
            }else if($judge == "D1"){
                $type = "大画-D1";
            }else{
                $type = "HiSpot-2";
            }
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
        $post_data = array(
                "touser"=>$openid,
                "template_id"=>"5ctmwIK_RFb3gV6Axi728nySARcKNztALFSTNtOUvmc",
                // "url"=>"http://www.baidu.com",
                "data"=> array(
                        "first" => array(
                            "value"=>"用户绑定登录成功通知",
                            "color"=>"#173177"
                            ),
                        "keyword1"=>array(
                            "value"=>$type,
                            "color"=>"#173177"
                            ),
                        "keyword2"=>array(
                            "value"=>$sn,
                            "color"=>"#173177"
                            ),
                        "keyword3"=> array(
                            "value"=>date('Y-m-d H:i:s',$time),
                            "color"=>"#173177"
                            ),
                        "remark"=> array(
                            "value"=>"欢迎开启美妙的电影之旅！",
                            "color"=>"#173177"
                            ),
                )
        );
        curl_post($url,json_encode($post_data));
    }
     function curl_post($url,$data){ // 模拟提交数据函数
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
    }
    //获取用户信息
    function get_openid_info($openid){
        $token = voucher_token();
        $access_token = $token['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        return json_decode(file_get_contents($url),true);
    }






 ?>